<?php
// +----------------------------------------------------------------------
// | Demo [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lmx0536.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: limx <715557344@qq.com> <http://www.lmx0536.cn>
// +----------------------------------------------------------------------
// | Date: 2016/4/26 Time: 20:10
// +----------------------------------------------------------------------

namespace Home\Controller;

use Home\Common\AuthController;

class CateController extends AuthController
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub、
        $this->check_admin_auth(array(1));
    }

    public function index()
    {
        $this->display();
    }

    public function add($id = "")
    {
        if (fnCheckInts($id)) {
            $M = D("Cate");
            $condition ["Id"] = $id;
            $res = $M->mfnFind($condition);
            if ($res) {
                $cate_info = unserialize($res["info"]);
                $this->assign("cate_info",$cate_info);
                $this->assign("info", $res);
            }
        }
        $this->display();
    }

    /**
     * [pfnDel 删除]
     * @author limx
     * @param $id
     */
    public function pfnDel($id = "")
    {
        if (fnCheckInts($id)) {
            $M = D("Cate");
            $res = $M->mfnDelete($id);
            if ($res) {
                $this->lfnAjaxSuccess();
            }
            $this->lfnAjaxFailed($M->getError());
        } else {
            $this->lfnAjaxFailed(C("SYSTEM_REFUSE_MSG"));
        }
    }

    /**
     * [pfnGetList 获取信息列表]
     * @author limx
     */
    public function pfnGetList()
    {
        if (IS_POST) {
            $M = D("Cate");
            $pageIndex = I("post.pageIndex");
            $pageSize = I("post.pageSize");
            $condition = " 1=1 ";
            $res = $M->mfnSelect($condition, $pageIndex, $pageSize);
            if ($res) {
                //拆解分类字段
                if(isset($res["data"])){
                    foreach($res["data"] as $key => $value){
                        $info = unserialize($value["info"]);
                        $cate_info="";
                        foreach($info as $i => $v){
                            $cate_info .= $v[1].",";
                        }
                        $cate_info = substr($cate_info,0,strlen($cate_info)-1);
                        $res["data"][$key]["info"]=$cate_info;
                    }
                }

                $this->lfnAjaxSuccess($res);
            }
            $this->lfnAjaxFailed($M->getError());
        }
        $this->lfnAjaxFailed(C("SYSTEM_ERROR_MSG"));
    }

    /**
     * [pfnAddCarLog 新增]
     * @author limx
     */
    public function pfnAdd()
    {
        if (IS_POST) {
            $cate_info = I("post.cate_info");

            $data["Id"] = I("post.id");
            $data["cate_name"] = I("post.cate_name");
            $data["info"] = serialize($cate_info);
            $data["admin_id"] = fnGetUserID();
            $M = D("Cate");
            $res = $M->mfnUpdate($data);
            if ($res !== false) {
                $this->lfnAjaxSuccess();
            } else {
                $this->lfnAjaxFailed($M->getError());
            }
        }
        $this->lfnAjaxFailed(C("SYSTEM_ERROR_MSG"));
    }

}